// 不可修改
#[allow(dead_code, bare_trait_objects)]
fn call_with_fn(x: i32, f: &dyn Fn(i32) -> i32) -> i32 {
    f(x)
}

// 修改多次
#[allow(dead_code, bare_trait_objects)]
fn call_with_fn_mut(x: i32, mut f: impl FnMut(i32) -> i32) -> i32 {
    f(x)
}

// 修改一次
#[allow(dead_code, bare_trait_objects)]
fn call_with_fn_once(x: i32, f: impl FnOnce(i32) -> ()) -> () {
    f(x)
}

#[allow(dead_code, unused_assignments)]
fn base() {
    let num = 5;
    let plus_num = |x: i32| num + x;
    println!("num:{}", plus_num(1));

    let mut num2 = 5;
    {
        let mut add_num = |x: i32| num2 += x;
        add_num(2);
    }
    println!("num2:{}", num2);

    let mut num3 = 6;
    {
        num3 = 2;
        println!("num3:{}", num3);
    }
    println!("num3:{}", num3);
}

fn main() {
    let mut j = 5;
    let f = |i: i32| i + j;
    let answer = call_with_fn(2, &f);
    println!("{}", answer);

    let f2 = |i: i32| i + j;
    let answer = call_with_fn_mut(3, f2);
    println!("{}", answer);

    let f2 =  |i: i32| j = i + j;
    call_with_fn_once(2, f2);
    println!("{}", j);

    let mut array: [i32; 3] = [0; 3];
    array[2] = 3;
    println!("{:?}", array);

    for i in &array {
        println!("{}", i);
    }

    //创建空Vec
    let v: Vec<i32> = Vec::new();
    //使用宏创建空Vec
    let v: Vec<i32> = vec![];
    //创建包含5个元素的Vec
    let v = vec![1, 2, 3, 4, 5];
    //创建十个零
    let v = vec![0; 10];
    //创建可变的Vec，并压入元素3
    let mut v = vec![1, 2];
    v.push(3);
    //创建拥有两个元素的Vec，并弹出一个元素
    let mut v = vec![1, 2];
    let two = v.pop();
    //创建包含三个元素的可变Vec，并索引一个值和修改一个值
    let mut v = vec![1, 2, 3];
    let three = v[2];
    v[1] = v[1] + 5;
    println!("{:?}", v);

    // 创建一个空的字符串
    let mut s = String::new();
    // 从 `&str` 类型转化成 `String` 类型
    let mut hello = String::from("Hello, ");
    // 压入字符和压入字符串切片
    hello.push('w');
    hello.push_str("orld!");

    // 弹出字符。19601。02
    let mut s = String::from("foo");
}